﻿/*----------------------------------------------------------------
// 公司名称：公司名称
// 
// 产品名称：Gaf
//
// 创 建 人：netcasewqs
// 创建日期：2022/3/4
// 邮    箱：netcasewqs@qq.com
//
// Copyright (C) 2022 Gaf，保留所有权利。
//
//----------------------------------------------------------------*/

namespace Gaf;

internal static class Log
{
  

    internal static bool Enabled = true;
    internal static TraceLevel Level = TraceLevel.Info;
    internal static string TraceFile = "gaf.log.txt";

    public static void Debug(string message)
    {
        WriteLine(TraceLevel.DBG, message);
    }

    public static void Debug(string message, params string[] args)
    {
        WriteLine(TraceLevel.DBG, string.Format(message, args));
    }

    public static void Info(string message)
    {
        WriteLine(TraceLevel.INF, message);
    }

    public static void Info(string message, params string[] args)
    {
        WriteLine(TraceLevel.INF, string.Format(message, args));
    }

    public static void Warn(string message)
    {
        WriteLine(TraceLevel.WRN, message);
    }

    public static void Warn(string message, Exception exception)
    {
        WriteLine(TraceLevel.WRN, message, exception);
    }

    public static void Error(string message)
    {
        WriteLine(TraceLevel.ERR, message);
    }

    public static void Error(string message, Exception exception)
    {
        WriteLine(TraceLevel.ERR, message, exception);
    }

    private static void WriteLine(TraceLevel level, string message, Exception? exception = null)
    {
        if (Enabled && level >= Level)
        {
            var threadName = Thread.CurrentThread.Name ?? Environment.CurrentManagedThreadId.ToString();

            if (exception == null)
            {
                message = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} [{level}] [{threadName}] {message} {Environment.NewLine}";
            }
            else
            {
                message = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} [{level}] [{threadName}] {message} {exception.Message} 异常堆栈信息:{exception.StackTrace} {Environment.NewLine}";
            }

            try
            {
                 File.AppendAllText(TraceFile, message);
                //Trace.Write(message);
            }
            catch { }
        }
        else
        {
#if DEBUG
            var threadName = Thread.CurrentThread.Name ?? Environment.CurrentManagedThreadId.ToString();

            if (exception == null)
            {
                message = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} [{level}] [{threadName}] {message} {Environment.NewLine}";
            }
            else
            {
                message = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} [{level}] [{threadName}] {message} {exception.Message} 异常堆栈信息:{exception.StackTrace} {Environment.NewLine}";
            }

            System.Diagnostics.Debug.Write(message);
#endif
        }
    }
}
